package sk.naive.talker.util;

import sk.naive.talker.props.*;

import java.util.*;

/**
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.10 $ $Date: 2007-05-07 10:34:20 $
 */
public class PropertyConfiguration {
	protected Set<String> settable;
	protected Set<String> persistent;
	protected Set<String> global;
	protected Map<String,Property> propertyObject;

	private Set<String> fixedSettable;
	private Set<String> fixedPersistent;
	private Set<String> fixedGlobal;

	public PropertyConfiguration(String resourceName) {
		settable = new TreeSet<String>();
		persistent = new TreeSet<String>();
		global = new TreeSet<String>();
		propertyObject = new HashMap<String,Property>();

		ResourceBundle rb = ResourceBundle.getBundle(resourceName);
		for (Enumeration<String> e = rb.getKeys(); e.hasMoreElements(); ) {
			String propName = e.nextElement();
			String[] vals = rb.getString(propName).split(",");
			for (String val1 : vals) {
				String val = val1.trim();
				if (val.equals("persistent")) {
					persistent.add(propName);
				} else if (val.equals("settable")) {
					settable.add(propName);
				} else if (val.equals("global")) {
					global.add(propName);
				} else {
					try {
						propertyObject.put(propName, (Property) Class.forName(val).newInstance());
					} catch (Exception ex) {
						Utils.unexpectedExceptionWarning("Problem with property '" + propName + "' class '" + val + "'.", ex);
					}
				}
			}
		}
		fixate();
	}

	protected void fixate() {
		fixedSettable = Collections.unmodifiableSet(settable);
		fixedPersistent = Collections.unmodifiableSet(persistent);
		fixedGlobal = Collections.unmodifiableSet(global);
	}

	public Set<String> getSettable() {
		return fixedSettable;
	}

	public Set<String> getPersistent() {
		return fixedPersistent;
	}

	public Set<String> getGlobal() {
		return fixedGlobal;
	}

	public Set<String> getPropertyNames() {
		return propertyObject.keySet();
	}

	public Property getProperty(String key) {
		Property p = propertyObject.get(key);
		if (p == null) {
			p = SimpleProperty.INSTANCE;
		}
		return p;
	}
}
